package me.zhengjie.modules.security.service.dto;

import lombok.Data;
import lombok.Getter;
import me.zhengjie.webuser.domain.WebUsers;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.sql.Timestamp;
import java.util.Collection;
import java.util.Collections;

@Data
public class WebJwtUserDto implements UserDetails {
    private Integer id;
    private String username;
    private String password;
    private String email;
    private boolean enabled;
    @Getter
    private String bio;
    @Getter
    private Timestamp createTime;

    public WebJwtUserDto(WebUsers user) {
        this.id = user.getId();
        this.username = user.getUsername();
        this.password = user.getPasswordHash();
        this.email = user.getEmail();
        this.bio = user.getBio();
        this.enabled = true; // 可以根据需要添加更多状态判断
        this.createTime = user.getCreatedAt();
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 默认给一个基本的USER角色，可以根据需要扩展
        return Collections.singletonList(new SimpleGrantedAuthority("ROLE_WEB_USER"));
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }
}